// api/image_api/image_list.go
package image_api

import (
	"github.com/gin-gonic/gin"
	"muziai-server/models"
	"muziai-server/service/common"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
)

type ImageListRequest struct {
	models.Page
	UserID uint `json:"userID" form:"userID"`
}

type ImageListResponse struct {
	models.ImageModel
	UserNickname string `json:"userNickname"`
	UserAvatar   string `json:"userAvatar"`
	WebPath      string `json:"webPath"`
}

func (ImageApi) ImageListView(c *gin.Context) {
	var cr ImageListRequest
	c.ShouldBindQuery(&cr)

	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)
	if claims.IsAuth() {
		cr.UserID = claims.UserID
	}

	_list, count, _ := common.QueryList(models.ImageModel{
		UserID: cr.UserID,
	}, common.Option{
		Page:     cr.Page,
		Likes:    []string{"filename"},
		Preloads: []string{"UserModel"},
		Sort:     "created_at desc",
	})
	var list = make([]ImageListResponse, 0)
	for _, model := range _list {
		list = append(list, ImageListResponse{
			ImageModel:   model,
			UserNickname: model.UserModel.Nickname,
			UserAvatar:   model.UserModel.Avatar,
			WebPath:      model.WebPath(),
		})
	}

	res.OkWithList(c, list, count)
}
